package asynqx

import (
	"context"
	"time"

	"gitee.com/lyuanbo/stock-plus/pkg/logger"
	"github.com/hibiken/asynq"
)

type Worker struct {
	Svc       *asynq.Server
	Mux       *asynq.ServeMux
	Scheduler *asynq.Scheduler
}

func New(_ context.Context, redisAddr, redisPassword string, lg *logger.Logger) *Worker {
	var worker Worker

	worker.Svc = asynq.NewServer(
		asynq.RedisClientOpt{Addr: redisAddr, Password: redisPassword, DB: 11},
		asynq.Config{
			Concurrency:     10,
			ShutdownTimeout: 20 * time.Second, // 等待任务完成的最大时间
			Logger:          &Lg{lg: lg},
		},
	)

	worker.Scheduler = asynq.NewScheduler(asynq.RedisClientOpt{Addr: redisAddr, Password: redisPassword, DB: 11}, &asynq.SchedulerOpts{
		LogLevel: asynq.InfoLevel,
	})
	worker.Mux = asynq.NewServeMux()
	return &worker
}
